<template>
  <view class="login-page">
    <!-- 背景装饰 -->
    <view class="bg-decoration">
      <view class="circle circle-1"></view>
      <view class="circle circle-2"></view>
      <view class="circle circle-3"></view>
    </view>
    
    <!-- 主要内容 -->
    <view class="login-container">
      <!-- Logo区域 -->
      <view class="logo-section">
        <view class="logo-icon">
          <text class="logo-text">隆</text>
        </view>
        <text class="company-name">隆晟建材</text>
        <text class="system-name">CRM管理系统</text>
      </view>
      
      <!-- 登录表单 -->
      <view class="login-form">
        <view class="form-title">用户登录</view>
        
        <view class="input-group">
          <view class="input-wrapper">
            <view class="input-icon">👤</view>
            <input 
              v-model="account" 
              placeholder="请输入账号" 
              class="input-field" 
              @input="clearError"
            />
          </view>
          
          <view class="input-wrapper">
            <view class="input-icon">🔒</view>
            <input 
              v-model="password" 
              placeholder="请输入密码" 
              password 
              class="input-field" 
              @input="clearError"
            />
          </view>
        </view>
        
        <!-- 错误提示区域 -->
        <view class="error-message" v-if="errorMessage">
          <view class="error-icon">⚠️</view>
          <text class="error-text">{{ errorMessage }}</text>
          <view class="error-close" @click="clearError">✕</view>
        </view>
        
        <button class="login-btn" @click="handleLogin">
          <text class="btn-text">登录系统</text>
        </button>
        
        <view class="login-tips">
          <text class="tip-text">请使用您的账号密码登录系统</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { loginApi } from '@/api/auth';

export default {
  data() {
    return {
      account: '',
      password: '',
      errorMessage: '' // 用于存储登录失败时的错误信息
    };
  },
  methods: {
    async handleLogin() {
      try {
        const res = await loginApi({ account: this.account, password: this.password });
        if (res.code === 1) {
          const role = res.data.userinfo.rule; // 获取角色数字
          
          // 保存角色信息到本地存储
          uni.setStorageSync('user-role', role);
          uni.setStorageSync('user-info', res.data);
          // 保存token到本地存储 - 从userinfo中获取token
          uni.setStorageSync('sc-token', res.data.userinfo.token);
          
          let url = '';
          // 根据角色数字跳转到对应页面
          switch (role) {
            case 1: // 销售
              url = '/pages/sales/index';
              break;
            case 2: // 财务
              url = '/pages/finance/index';
              break;
            case 3: // 生产
              url = '/pages/production/index';
              break;
            case 4: // 经理
              url = '/pages/boss/dashboard'; // 经理跳转到数据统计页面
              break;
            default:
              url = '/pages/sales/index'; // 默认跳转到销售页面
              break;
          }
          
          uni.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1500
          });
          
          // 延迟跳转，让用户看到成功提示
          setTimeout(() => {
            uni.reLaunch({ url });
          }, 1500);
          
        } else {
          this.errorMessage = res.msg || '登录失败'; // 设置错误信息
          uni.showToast({ 
            title: res.msg || '登录失败', 
            icon: 'none' 
          });
        }
      } catch (error) {
        console.error('登录错误:', error);
        this.errorMessage = '登录失败，请重试'; // 设置错误信息
        uni.showToast({ 
          title: '登录失败，请重试', 
          icon: 'none' 
        });
      }
    },
    clearError() {
      this.errorMessage = '';
    }
  }
};
</script>
<style lang="scss" scoped>
/* 登录页面整体样式 */
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40rpx;
  box-sizing: border-box;
}

/* 背景装饰圆圈 */
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.circle-1 {
  width: 300rpx;
  height: 300rpx;
  top: -150rpx;
  right: -150rpx;
  animation: float 6s ease-in-out infinite;
}

.circle-2 {
  width: 200rpx;
  height: 200rpx;
  bottom: -100rpx;
  left: -100rpx;
  animation: float 8s ease-in-out infinite reverse;
}

.circle-3 {
  width: 150rpx;
  height: 150rpx;
  top: 50%;
  right: 10%;
  animation: float 7s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

/* 主要内容容器 */
.login-container {
  width: 100%;
  max-width: 600rpx;
  z-index: 2;
  position: relative;
}

/* Logo区域 */
.logo-section {
  text-align: center;
  margin-bottom: 80rpx;
}

.logo-icon {
  width: 120rpx;
  height: 120rpx;
  background: linear-gradient(135deg, #ff6b6b, #ee5a24);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 30rpx;
  box-shadow: 0 20rpx 40rpx rgba(238, 90, 36, 0.3);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.logo-text {
  font-size: 48rpx;
  font-weight: bold;
  color: white;
}

.company-name {
  font-size: 48rpx;
  font-weight: bold;
  color: white;
  display: block;
  margin-bottom: 16rpx;
  text-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.2);
}

.system-name {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.8);
  display: block;
  font-weight: 300;
}

/* 登录表单 */
.login-form {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24rpx;
  padding: 60rpx 40rpx;
  box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.form-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 50rpx;
}

/* 输入框组 */
.input-group {
  margin-bottom: 50rpx;
}

.input-wrapper {
  position: relative;
  margin-bottom: 30rpx;
  background: #f8f9fa;
  border-radius: 16rpx;
  border: 2rpx solid transparent;
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 6rpx rgba(102, 126, 234, 0.1);
}

.input-icon {
  position: absolute;
  left: 24rpx;
  top: 50%;
  transform: translateY(-50%);
  font-size: 32rpx;
  z-index: 2;
}

.input-field {
  width: 100%;
  height: 88rpx;
  padding: 0 24rpx 0 80rpx;
  border: none;
  background: transparent;
  font-size: 28rpx;
  color: #333;
  box-sizing: border-box;
}

.input-field::placeholder {
  color: #999;
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 88rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 16rpx;
  color: white;
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 30rpx;
  transition: all 0.3s ease;
  box-shadow: 0 8rpx 24rpx rgba(102, 126, 234, 0.3);
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.login-btn:active::before {
  left: 100%;
}

.login-btn:active {
  transform: translateY(2rpx);
  box-shadow: 0 4rpx 12rpx rgba(102, 126, 234, 0.4);
}

.btn-text {
  position: relative;
  z-index: 1;
}

/* 登录提示 */
.login-tips {
  text-align: center;
}

.tip-text {
  font-size: 24rpx;
  color: #999;
  line-height: 1.5;
}

/* 错误提示区域 */
.error-message {
  display: flex;
  align-items: center;
  background-color: #ffebee; /* 浅红色背景 */
  border: 1px solid #ef5350; /* 红色边框 */
  border-radius: 12rpx;
  padding: 15rpx 20rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 8rpx rgba(239, 83, 80, 0.2);
  position: relative;
}

.error-icon {
  font-size: 36rpx;
  color: #ef5350; /* 红色图标 */
  margin-right: 15rpx;
  flex-shrink: 0;
}

.error-text {
  font-size: 28rpx;
  color: #ef5350; /* 红色文字 */
  font-weight: bold;
  flex: 1;
}

.error-close {
  font-size: 32rpx;
  color: #999;
  margin-left: 15rpx;
  cursor: pointer;
  width: 40rpx;
  height: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s ease;
  flex-shrink: 0;
}

.error-close:hover {
  background-color: #f5f5f5;
  color: #666;
}

.error-close:active {
  background-color: #e0e0e0;
  transform: scale(0.95);
}

/* 响应式设计 */
@media (max-width: 750rpx) {
  .login-container {
    max-width: 100%;
  }
  
  .login-form {
    padding: 50rpx 30rpx;
  }
  
  .company-name {
    font-size: 40rpx;
  }
  
  .system-name {
    font-size: 24rpx;
  }
}
</style>